package com.xbs.controller.user;
/**
 * 登录界面Servlet
 */
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.UserEntity;
import com.xbs.service.user.LoginService;
@WebServlet("/login")
public class LoginServlet extends BaseServlet {
	private UserEntity user;
	private LoginService ls;
	private HashMap<String, Object> map;
	private boolean jg;
	public LoginServlet() {
		user = new UserEntity();
		ls = new LoginService();
		map = new HashMap<>();
		jg=true;
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		forward("userview/login");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//获取用户填写的账户和密码
		String tel = req.getParameter("tel");
		String pwd = req.getParameter("pwd");
		
		String error="";
		//服务器端验证是否为空
		if(tel==null||tel.isEmpty()){
			error="手机号不能为空";
			jg=false;
		}
		if(pwd==null||pwd.isEmpty()){
			error="密码不能为空";
			jg=false;
		}
		//从数据库中获取该用户的密码
		user = ls.getPwd(tel);
		if(jg){
			//验证密码是否正确
			if(user.getUser_password().equals(pwd)){
				//获取该用户的姓名
				map.put("uname", user.getUser_name());
				map.put("ucard",user.getUser_idcard());
				map.put("utel", user.getUser_telphone());
				map.put("uemail",user.getUser_email());
				map.put("status", 1);
				redirect("/index",map);
			}else{
				error="账号或密码错误";
				
				req.setAttribute("error", error);
				forward("userview/login");
			}
		}
		
		
	}
}
